package com.zmn.plat.admin.controller.common.company;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.model.entity.company.BaseCompany;
import com.zmn.plat.model.entity.company.BaseCompanyQuery;
import com.zmn.plat.services.interfaces.company.BaseCompanyService;

/**
 * @author nowind
 * @since 17/11/10 11:48
 */
@Controller
@RequestMapping("/common/company/")
public class CommonCompanyController extends PlatBaseController {

    @Resource
    BaseCompanyService baseCompanyService;

    /**
     * region 公司区域联动
     *
     * @param response
     * @param type
     */
    @RequestMapping(value = "listCompany")
    public void listCompany(HttpServletResponse response, @RequestParam(value = "parentId", required = false) Integer type) {
        BaseCompanyQuery query = new BaseCompanyQuery();
        query.setStatus(GlobalConsts.YES);

        List<BaseCompany> items = baseCompanyService.listByQuery(query);

        StringBuilder sb = new StringBuilder("[");
        String jsonFormat = "[\"%s\",\"%s\"]%s ";

        for (int i = 0; i < items.size(); i++) {
            BaseCompany item = items.get(i);
            String companyName = item.getName();
            Integer companyId = item.getCompanyId();
            sb.append(String.format(jsonFormat, companyId, companyName, (i == items.size() - 1 ? "" : ",")));
        }
        sb.append("]");

        ResponseUtil.outWrite(response, sb.toString());
    }

    /**
     * region 子公司服务公司联动
     *
     * @param response
     * @param parentId
     */
    @RequestMapping(value = "listManageCompanyByBizCompanyId")
    public void listManageCompanyByBizCompanyId(HttpServletResponse response,
        @RequestParam(value = "parentId", required = false) Integer parentId) {
        BaseCompanyQuery query = new BaseCompanyQuery();
        query.setSubCompanyId(parentId);
        List<BaseCompany> items = baseCompanyService.listByQuery(query);

        StringBuilder sb = new StringBuilder("[");
        String jsonFormat = "[\"%s\",\"%s\"]%s ";

        for (int i = 0; i < items.size(); i++) {
            BaseCompany item = items.get(i);
            String companyName = item.getName();
            Integer companyId = item.getCompanyId();
            sb.append(String.format(jsonFormat, companyId, companyName, (i == items.size() - 1 ? "" : ",")));
        }
        sb.append("]");

        ResponseUtil.outWrite(response, sb.toString());
    }
}
